
package com.babble.blog.rabbitCustomer;

import com.alibaba.fastjson.JSONObject;
import com.babble.blog.entity.Blog;
import com.babble.blog.service.IBlogService;
import com.babble.subitem.feign.ISubjectClient;
import com.babble.subitem.feign.ISubjectItemClient;
import com.rabbitmq.client.Channel;
import org.springblade.common.exception.MsgException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class MyAckReceiver implements ChannelAwareMessageListener {
    @Autowired
	IBlogService blogService;
    @Autowired
	ISubjectItemClient subjectItemClient;
    @Autowired
	ISubjectClient subjectClient;

	/**
     * 处理单个队列
     * @param message
     * @param
     * @throws IOException
     */
    @Override
    public void onMessage(Message message, Channel channel) throws IOException, MsgException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
		String json = new String(message.getBody(), "UTF-8");
		try {
			Blog blog = JSONObject.parseObject(json, Blog.class);
			blog.setCreateTime(LocalDateTime.now());
			blog.setUpdateTime(LocalDateTime.now());
			//添加博客
			blogService.save(blog);
			channel.basicAck(deliveryTag, true);
            //第二个参数，true会重新放回队列，所以需要自己根据业务逻辑判断什么时候使用拒绝
			//channel.basicReject(deliveryTag, true);
        } catch (IOException e) {
            channel.basicReject(deliveryTag, false);
            e.printStackTrace();
        }
    }
    @Override
    public void onMessage(Message message) {

    }

    @Override
    public void onMessageBatch(List<Message> messages, Channel channel) {

    }
}

